﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SQLExpressDAL;

public partial class Manageweb_Stock_StockDeal : System.Web.UI.Page
{

    private Decimal _accountMoney;

    public Decimal AccountMoney
    {
        get
        {
            return _accountMoney;
        }
        set
        {
            _accountMoney = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserID"]==null)
        {
            Response.Redirect("~/MemberLogin.aspx");
        }

        if (!Page.IsPostBack)
        {
            
            BindData();
            BindDealRecords();
            InitAccountMoney();
        }

    }

    private void InitAccountMoney()
    {
        SQLExpressDAL.StockTableAdapters.RecordTableAdapter recordAdp = new SQLExpressDAL.StockTableAdapters.RecordTableAdapter();
        AccountMoney=Convert.ToDecimal(  recordAdp.QueryAccountMoney(Session["UserID"].ToString()));

    }


    protected void BindData()
    {
        SQLExpressDAL.StockTableAdapters.HoldStockInfoTableAdapter adp = new SQLExpressDAL.StockTableAdapters.HoldStockInfoTableAdapter();

        this.GV_StockInfoList.DataSource = adp.GetRecordInfo("a");
        this.GV_StockInfoList.DataBind();


    }

    protected void BindDealRecords() {
        SQLExpressDAL.StockTableAdapters.DealRecordTableAdapter adp = new SQLExpressDAL.StockTableAdapters.DealRecordTableAdapter();

        this.GVDealRecord.DataSource = adp.GetDealRecords(Session["UserID"].ToString());
        this.GVDealRecord.DataBind();
    
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        String stockCode = Request["txtStockCode"];
        String stockName = Request["txtStockName"];
        Decimal stockPrice = Convert.ToDecimal(Request["txtPrice"]);
        Int32 stockCount = Convert.ToInt32(this.txtCount.Text.Trim());
        Decimal averagePrice = Convert.ToDecimal(this.hidAveragePrice.Value);
        int RecordCount = 0;

        SQLExpressDAL.StockTableAdapters.RecordTableAdapter recordAdp = new SQLExpressDAL.StockTableAdapters.RecordTableAdapter();

        RecordCount = Convert.ToInt32(recordAdp.CheckStockCount(Session["UserID"].ToString(), stockCode));

        if ((stockCount > 0) && (stockCount / RecordCount) <= 0.03)
        {
            int flag = recordAdp.InsertBuyRecord(Session["UserID"].ToString(), stockCode, stockName, stockPrice, stockCount, "OUT", averagePrice,"",0);
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", "alert('交易禁止!\n\r系统要求每次交易最大比例为70%.');", true);
        }

        BindData();
        ClearTxt();
    }


    protected void ClearTxt()
    {
        this.txtCount.Text = String.Empty;
    }

    protected void GVDealRecord_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GVDealRecord.PageIndex = e.NewPageIndex;
        BindDealRecords();
    }

    protected void GV_StockInfoList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GV_StockInfoList.PageIndex = e.NewPageIndex;
        BindData();
    }
}
